package com.wonder.sms.ucpass;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wonder.admin.service.SysConfigService;
import com.wonder.common.utils.StringUtils;

import lombok.extern.java.Log;

@Service
@Log
public class UcPassMessageSendService {

	@Autowired
	SysConfigService sysConfigService;

	@Autowired
	RestTemplate restTemplate;

	public String sendMessage(String modile, String code, String uid) {
		log.info("sendMessage start," + getParameter(modile, code, uid,1));
		// 单发邮件API
		String url = sysConfigService.getCacheKey("ucpass_url") + "sendsms";
		// 使用restTemplate进行访问远程服务
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());

		HttpEntity<String> httpEntity = new HttpEntity<String>(getParameter(modile, code, uid,1), headers);
		String result= restTemplate.postForObject(url, httpEntity, String.class);
		JSONObject jsonObject=JSON.parseObject(result);
		//String result= restTemplate.patchForObject(url, httpEntity, String.class);
		if(jsonObject.get("code").toString().equals("000000")) return "ok";
		return result;
	}

	public String sendBatchMessage(String modile,String code,String uid) {
	    	 log.info("sendMessage start,"+getParameter(modile,code,uid,2));

	        //群发邮件API
	        String url =sysConfigService.getCacheKey("ucpass_url")+"sendsms_batch";
	        HttpHeaders headers = new HttpHeaders();
			MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
	        headers.setContentType(type);
	        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
	        
	        HttpEntity<String> httpEntity = new HttpEntity<String>(getParameter(modile,code,uid,2),headers);
	        String result= restTemplate.postForObject(url, httpEntity, String.class);
			JSONObject jsonObject=JSON.parseObject(result);
			if(jsonObject.get("code").toString().equals("000000")) return "ok";
			return result;
	    }

	/**
	 * 配置发送参数
	 *
	 * @param messageConfig
	 * @return
	 */
	public String getParameter(String phone, String code, String uid,int type) {
		JSONObject jsonObject = new JSONObject();
//	        # 短信服务
//	        # 应用ID
//	        message.appId= xxxxxxxxxxxxxxxxxxxx
//	        # 用户sid
//	        message.account.sid= xxxxxxxxxxxxxxxxxxxx
//	        # 鉴权token
//	        message.auth.token= xxxxxxxxxxxxxxxxxxxx
//	        # 请求地址
//	        message.rest.url= https://open.ucpaas.com/ol/sms/{function}
//	        # 默认手机号
//	        message.default.phone = xxxxxxxxxxx
//	        # 默认模板
//	        message.defaule.templateId = xxxxxx

		// 基础配置
		jsonObject.put("sid", sysConfigService.getCacheKey("ucpass_sid"));
		jsonObject.put("token", sysConfigService.getCacheKey("ucpass_token"));
		jsonObject.put("appid", sysConfigService.getCacheKey("ucpass_appid"));
		// 模板ID 参数之间用逗号作为间隔符
		if(type==1)
		   jsonObject.put("templateid", sysConfigService.getCacheKey("ucpass_template"));
		//群发模版
		if(type==2)
		   jsonObject.put("templateid", sysConfigService.getCacheKey("ucpass_group_template"));
		// 要发送的手机号,多个手机号用逗号分隔
		jsonObject.put("mobile", phone);
		// 要发送的验证码,多个验证码用逗号分隔
		jsonObject.put("param", code);
		// 要发送的UID
		if (StringUtils.hasText(uid))
			jsonObject.put("uid", uid);
		// 用户ID，随状态报告返回,可以不填写
		return JSONObject.toJSONString(jsonObject);

	}

}
